package com.example.demo.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/*
    第二步: 将自定义拦截器放入系统配置中, 并设置拦截规则
 */

@Configuration
public class AppConfig implements WebMvcConfigurer {

    @Autowired
    private LoginIntercept loginIntercept;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //添加拦截路径
        registry.addInterceptor(loginIntercept).
                addPathPatterns("/**").// 拦截所有url
                excludePathPatterns("/user/login").// login不拦截
                excludePathPatterns("/user/reg").// register不拦截
                excludePathPatterns("/login.html").// 界面不拦截
                excludePathPatterns("/reg.html").
                excludePathPatterns("/**/*.css").
                excludePathPatterns("/**/*.js").
                excludePathPatterns("/**/*.png").
                excludePathPatterns("/**/*.jpg");

        // 可以添加多个拦截器
        registry.addInterceptor(loginIntercept);
    }

    //添加前缀, 比如localhost:8080/api/user/login
    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        configurer.addPathPrefix("api", c -> true);
    }
}
